import kboneAPI from 'kbone-api';

const chatRandomArr = '0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz';

export default class SystemUtil {
  static getRootFontSize() {
    let fontSize = 16;
    fontSize = window.screen.width / 750 * fontSize;
    fontSize = Math.round(fontSize);
    return fontSize;
  }

  static alert(msg, title) {
    return new window.Promise((resolve, reject) => {
      kboneAPI.showModal({
        title: title || '信息',
        content: msg,
        showCancel: false,
        success: res => {
          resolve(res);
        },
        fail: error => {
          reject(error);
        }
      });
    });
  }

  static isEmpty(value) {
    if (value === null || value === undefined) {
      return true;
    }

    let type = typeof value;
    if (type === 'boolean') {
      return value;
    }

    if (type === 'number') {
      return isNaN(value);
    }

    if (type === 'string') {
      return value === '';
    }

    if (type === 'object') {
      if (value instanceof Array) {
        return value.length === 0;
      }
      return this.isEmptyObjct(value);
    }

    return false;
  }

  static isEmptyObjct(object) {
    if (object === null || object === undefined) {
      return true;
    }

    for (let key in object) {
      return false;
    }
    return true;
  }

  static groupArr(array, field) {
    let groupData = {};
    for (let i = 0; i < array.length; i++) {
      const obj = array[i];
      let value = obj[field];
      let data = groupData[value];
      if (data === undefined) {
        data = [];
        groupData[value] = data;
      }
      data.push(obj);
    }
    return groupData;
  }

  static getId() {
    let s = [];
    let hexDigits = "0123456789abcdef";
    for (let i = 0; i < 32; i++) {
      s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[12] = "4";
    s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1);
    let uuid = s.join("");
    return uuid;
  }

  static formatPx(value) {
    let data;
    if (process.env.isMiniprogram) {
      data = value + 'rpx';
    } else {
      data = value / 16 + 'rem';
    }
    return data;
  }

  static getPropByPath(obj, path, defaultValue) {
    let pathArr = path.split('.');
    for (let i = 0; i < pathArr.length; i++) {
      const field = pathArr[i];
      if (obj === undefined) {
        break;
      }
      obj = obj[field];
    }
    if (obj === undefined) {
      return defaultValue;
    }
    return obj;
  }

  static roundValue(value, precision = 0) {
    let num = Math.pow(10, precision);
    value = Math.round(value * num) / num;
    return value;
  }

  static floorValue(value, precision = 0) {
    let num = Math.pow(10, precision);
    value = this.roundValue(value, precision + 2);
    value = Math.floor(value * num) / num;
    return value;
  }

  static ceilValue(value, precision = 0) {
    let num = Math.pow(10, precision);
    value = this.roundValue(value, precision + 2);
    value = Math.ceil(value * num) / num;
    return value;
  }

  static formatFormParam(obj, hasNull = true) {
    let paramList = [];
    for (let key in obj) {
      let value = obj[key];
      if (this.isEmpty(value)) {
        if (!hasNull) {
          continue;
        }
        value = '';
      }
      paramList.push(`${key}=${value}`);
    }
    return paramList.join('&');
  }

  static stringToArrayBuffer(str) {
    let buf = new ArrayBuffer(str.length * 2);
    let bufView = new Uint16Array(buf);
    for (let i = 0, strLen = str.length; i < strLen; i++) {
      bufView[i] = str.charCodeAt(i);
    }
    return bufView;
  }

  static showLoading(title = '加载中...') {
    kboneAPI.showLoading({
      mask: true,
      title: title
    });
  }

  static hideLoading() {
    kboneAPI.hideLoading();
  }

  static getRandomStr(count) {
    let strList = [];
    for (let i = 0; i < count; i++) {
      let index = Math.floor(Math.random() * chatRandomArr.length);
      strList.push(chatRandomArr[index]);
    }
    return strList.join('');
  }

  static getRadomArrayData(arr = [], count = 1) {
    if (arr.length <= count) {
      return arr;
    }

    let arrDataTemp = [];
    for (let i = 0; i < count;) {
      let index = Math.floor(Math.random() * arr.length);
      if (arrDataTemp[index] !== undefined) {
        continue;
      }
      arrDataTemp[index] = arr[index];
      i++;
    }

    let arrData = [];
    for (let i = 0; i < arrDataTemp.length; i++) {
      const obj = arrDataTemp[i];
      if (obj !== undefined) {
        arrData.push(obj);
      }
    }
    return arrData;
  }

  static compareVersion(v1, v2) {
    v1 = v1.split('.');
    v2 = v2.split('.');
    const len = Math.max(v1.length, v2.length);

    while (v1.length < len) {
      v1.push('0');
    }
    while (v2.length < len) {
      v2.push('0');
    }

    for (let i = 0; i < len; i++) {
      const num1 = parseInt(v1[i]);
      const num2 = parseInt(v2[i]);

      if (num1 > num2) {
        return 1;
      } else if (num1 < num2) {
        return -1;
      }
    }

    return 0;
  }
}
